package com.smartStatus.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.smartStatus.admin.service.*;
import com.smartStatus.admin.utils.JoinUtil;
import com.smartStatus.api.entity.*;
import com.smartStatus.api.vo.SysGradeVO;
import com.smartStatus.core.constant.StatusUser;
import com.smartStatus.core.constant.SysUser;
import com.smartStatus.security.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartStatus.core.util.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统年级表 Controller
 *
 * @author lby
 * @since 2021-03-06
 */
@RestController
@Slf4j
@Api(value = "SysGradeController", tags = "SysGrade管理")
@AllArgsConstructor
@RequestMapping("/SysGrade")
public class SysGradeController {

    private final SysGradeService sysGradeService;
    private final GradeClassService gradeClassService;
    private final SysClassesService sysClassesService;
    private final SysUserTenantService sysUserTenantService;
    private final TenantGradeService tenantGradeService;

    /**
     * 分页数据
     */
    @PreAuthorize("@pms.hasPermission('sys_grade_view')")
    @ApiOperation(value = "公共参数分页数据")
    @GetMapping("/page")
    public R sysGradePage(Page<SysGrade> page, SysGrade sysGrade) {
        QueryWrapper<SysGrade> query = Wrappers.query();
        List<Integer> idList = new ArrayList<>();
        Integer tenantId = SecurityUtils.getUser().getTenantId();
        if (tenantId != null && tenantId != 0) {
            QueryWrapper<TenantGrade> query2 = Wrappers.query();
            query2.eq("tenant_id", tenantId);
            List<TenantGrade> list = tenantGradeService.list(query2);
            list.forEach(tenantGrade -> {
                idList.add(tenantGrade.getGradeId());
            });
            query.in("id", idList);
        }
        Page page1 = JoinUtil.page(sysGradeService.page(page, query), SysGradeVO.class);
        // 年级绑定班级
        List<SysGradeVO> records = (List<SysGradeVO>) page1.getRecords();
        records.forEach(sysGradeVO -> {
            List<SysClasses> list1 = new ArrayList<>();
            QueryWrapper<GradeClass> query1 = Wrappers.query();
            query1.eq("grade_id", sysGradeVO.getId());
            List<GradeClass> list = gradeClassService.list(query1);
            list.forEach(gradeClass -> {
                SysClasses byId = sysClassesService.getById(gradeClass.getClassId());
                list1.add(byId);
            });
            sysGradeVO.setClassNumber(list.size());
            sysGradeVO.setClassesList(list1);
        });
        return R.ok(page1);
    }


    /**
     * 列表数据
     */
    @PreAuthorize("@pms.hasPermission('sys_grade_view')")
    @ApiOperation(value = "公共参数列表")
    @GetMapping("/list")
    public R sysGradeList() {
        return R.ok(sysGradeService.list());
    }


    /**
     * 编辑数据
     */
    @PreAuthorize("@pms.hasPermission('sys_grade_view')")
    @ApiOperation(value = "编辑公共参数")
    @ApiImplicitParam(name = "SysGrade", value = "SysGrade实体", required = true, dataType = "SysGrade")
    @PutMapping("/edit")
    public R sysGradeUpdate(@RequestBody SysGrade sysGrade) {
        return R.ok(sysGradeService.updateById(sysGrade));
    }


    /**
     * 删除数据
     */
    @PreAuthorize("@pms.hasPermission('sys_grade_view')")
    @ApiOperation(value = "删除公共参数")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @DeleteMapping("/{id}")
    public R sysGradeDelete(@PathVariable Integer id) {
        return R.ok(sysGradeService.removeById(id));
    }

    /**
     * 添加数据
     */
    @PreAuthorize("@pms.hasPermission('sys_grade_view')")
    @ApiOperation(value = "添加公共参数")
    @ApiImplicitParam(name = "SysGrade", value = "SysGrade实体", required = true, dataType = "SysGrade")
    @PostMapping("/add")
    public R sysGradeInsert(@RequestBody SysGrade sysGrade) {
        // 添加年级
        sysGradeService.save(sysGrade);
        // 添加年级，租户关联
        TenantGrade tenantGrade = new TenantGrade();
        tenantGrade.setTenantId(SecurityUtils.getUser().getTenantId());
        tenantGrade.setGradeId(sysGrade.getId());
        return R.ok(tenantGrade.insert());
    }


    /**
     * 根据年级获取班级列表
     *
     * @param gradeId
     * @return
     */
    @PreAuthorize("@pms.hasPermission('sys_grade_view')")
    @ApiOperation(value = "根据年级获取班级列表")
    @ApiImplicitParam(name = "SysGrade", value = "SysGrade实体", required = true, dataType = "SysGrade")
    @GetMapping("/getClass-by-id/{gradeId}")
    public R getClassByGradeId(@PathVariable("gradeId") Integer gradeId) {
        QueryWrapper<GradeClass> query = Wrappers.query();
        query.eq("garde_id", gradeId);
        List<GradeClass> list = gradeClassService.list(query);
        List<Integer> idLists = new ArrayList<>();
        list.forEach(gradeClass -> {
            idLists.add(gradeClass.getClassId());
        });
        return R.ok(sysClassesService.listByIds(idLists));
    }

}
